#include "component/broker/server/RequestProcessor.h"

namespace zeroplus::mq
{
    RequestProcessor::RequestProcessor()
        : mediator_(std::make_shared<BrokerMediator>()),
          publishService_(std::make_shared<PubService>(mediator_)),
          subscribeService_(std::make_shared<SubService>(mediator_)),
          outsideService_(std::make_shared<OutsideService>(mediator_)),
          adminService_(std::make_shared<AdminService>(mediator_))
    {
        // 注册命令
        outsideService_->AddRequestCode(transport::RequestCode::PUB_MESSAGE, publishService_);
        outsideService_->AddRequestCode(transport::RequestCode::SUB_MESSAGE, subscribeService_);
        outsideService_->AddRequestCode(transport::RequestCode::SEND_ACK, subscribeService_);
        outsideService_->AddRequestCode(transport::RequestCode::UPDATE_AND_CREATE_TOPIC, adminService_);
        // 注册服务
        mediator_->AddService(ServiceType::PublishService, publishService_);
        mediator_->AddService(ServiceType::SubscribeService, subscribeService_);
        mediator_->AddService(ServiceType::AdministrationService, adminService_);
        mediator_->AddService(ServiceType::ReceiveService, outsideService_);
    }

    RequestProcessor::~RequestProcessor()
    {
    }

    bool RequestProcessor::Start()
    {
        std::thread worker_ = std::thread(std::bind(&OutsideService::OutSideServicehandler, outsideService_.get()));
        // reactor_.Add(*outsideService_->GetSocket(), std::bind(&OutsideService::OutSideServicehandler, outsideService_.get()), zmqpp::poller::poll_in);
        reactor_.Add(*subscribeService_->GetSocket(), std::bind(&SubService::PublishHandler, subscribeService_.get()), zmqpp::poller::poll_out);
        reactor_.Start();
        worker_.join();
    }

    bool RequestProcessor::Stop()
    {
    }

}
